<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\TcmBook;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TcmBookController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TcmBook(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->column('author');
            $grid->column('dynasty');
            $grid->column('name');
            $grid->column('img')->image(' ', 50);
            $grid->column('share_img')->image(' ', 50);
            $grid->column('sort');
            $grid->column('sound')->using(['否', '是'])->label(['warning', 'primary']);
            $grid->column('status')->switch();
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a style="margin-right:5px" href="' . admin_route('tcmbook_chapter.index', ['book_id' => $actions->row->id]) . '">章节目录</a>');
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->like('name')->width(3);
                $filter->between('created_at')->datetime()->width(4);
            });

            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TcmBook(), function (Show $show) {
            $show->field('id');
            $show->field('author');
            $show->field('dynasty');
            $show->field('name');
            $show->field('img');
            $show->field('share_img');
            $show->field('sort');
            $show->field('sound');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TcmBook(), function (Form $form) {
            $form->display('id');
            $form->text('author');
            $form->text('dynasty');
            $form->text('name');
            $form->image('img')->uniqueName()->autoUpload()->saveFullUrl()->saving(function ($v){
                return (string)$v;
            });
            $form->image('share_img')->uniqueName()->autoUpload()->saveFullUrl()->saving(function ($v){
                return (string)$v;
            });
            $form->number('sort');
            $form->radio('sound')->options(['否', '是'])->default(0);
            $form->radio('status')->options(['否', '是'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
